//
//  LowMallViewController.m
//  JiuLe
//
//  Created by SunChao on 16/10/28.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "LowMallViewController.h"
#import "MyGradesViewController.h"
#import "OrderViewController.h"
#import "GoodsDetailViewController.h"
#import "GoodsCell.h"
#import "GoodsModel.h"
#import "MallRequestManager.h"
#import "UIImageView+WebCache.h"
#import "MallCell.h"
@interface LowMallViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource0;
@property (nonatomic, strong) NSMutableArray *dataSource1;
@property (nonatomic, strong) NSMutableArray *dataSource2;
@end

@implementation LowMallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
}
- (void)requestData
{
    NSMutableDictionary *params = [NSMutableDictionary new];
    NSString *userID = [[NSUserDefaults standardUserDefaults]objectForKey:@"userID"];
    [params setObject:userID forKey:@"user_id"];
    NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
    [params setObject:token forKey:@"token"];
    NSNumber *typeid = [NSNumber numberWithInt:0];
    [params setObject:typeid forKey:@"typeid"];
    [SVProgressHUD showWithStatus:@"加载中.." maskType:SVProgressHUDMaskTypeClear];
    [MallRequestManager post:@"ShoppingMall/querygoodslist" RequestParams:params FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [SVProgressHUD dismiss];
        if (connectionError) {
            NSLog(@"connectionError:%@",connectionError);
        }else
        {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];//转换数据格式
            if ([dic.allKeys containsObject:@"result"]) {
                NSArray *result = [dic objectForKey:@"result"];
                NSLog(@"result:%@",result);
                self.dataSource0 = [NSMutableArray new];
                self.dataSource1 = [NSMutableArray new];
                self.dataSource2 = [NSMutableArray new];
                for (NSDictionary *dic in result) {
                    if ([dic.allKeys containsObject:@"good_type"]) {
                        if ([dic[@"good_type"]isEqualToString:@"3"]) {
                            [self.dataSource2 addObject:[GoodsModel appWithDict:dic]];
                        }
                        if ([dic[@"good_type"]isEqualToString:@"2"]) {
                            [self.dataSource1 addObject:[GoodsModel appWithDict:dic]];
                        }
                        if ([dic[@"good_type"]isEqualToString:@"1"]) {
                            [self.dataSource0 addObject:[GoodsModel appWithDict:dic]];
                        }
                    }
                }
                
                [self performSelectorOnMainThread:@selector(createUIWithData:) withObject:nil waitUntilDone:NO];
            }
        }
    }];
}
- (void)createUIWithData:(NSArray *)array
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate= self;
    self.tableView.dataSource= self;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"MallCell" bundle:nil] forCellReuseIdentifier:@"CellID"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellID = @"myCell";
    MallCell *cell =[tableView dequeueReusableCellWithIdentifier:CellID];
    CALayer *layer = [cell.imgView layer];
    layer.borderColor = [UIColor grayColor].CGColor;
    layer.borderWidth = 0.5f;
    switch (indexPath.section) {
        case 0:
        {
            GoodsModel *model  = self.dataSource0[indexPath.item];
            [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"商城首页默认图片"]];
//            cell.priceLabel.text = [NSString stringWithFormat:@"￥%@",model.price];
//            cell.goodsInfoLabel.text = model.title;
//            NSLog(@"model:%@",model.goodsId);
            //已下架
            if ([model.status isEqualToString:@"0"]) {
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_down"];
                //新品
            }else if ([model.status isEqualToString:@"1"]){
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_new"];
                //热卖中
            }else if ([model.status isEqualToString:@"2"]){
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_hot"];
                //缺货中
            }else if ([model.status isEqualToString:@"3"]){
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_Stockout"];
            }
            
            return cell;
            
        }
            break;
        case 1:
//        {
//            GoodsModel *model  = self.dataSource1[indexPath.item];
//            [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"商城首页默认图片"]];
//            cell.priceLabel.text = [NSString stringWithFormat:@"￥%@",model.price];
//            cell.goodsInfoLabel.text = model.title;
//            NSLog(@"model:%@",model.goodsId);
//            //已下架
//            if ([model.status isEqualToString:@"0"]) {
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_down"];
//                //新品
//            }else if ([model.status isEqualToString:@"1"]){
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_new"];
//                //热卖中
//            }else if ([model.status isEqualToString:@"2"]){
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_hot"];
//                //缺货中
//            }else if ([model.status isEqualToString:@"3"]){
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_Stockout"];
//            }
//            return cell;
//            
//        }
            break;
        case 2:
//        {
//            GoodsModel *model  = self.dataSource2[indexPath.item];
//            [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"商城首页默认图片"]];
//            float price = [model.discountprice floatValue];
//            cell.priceLabel.text = [NSString stringWithFormat:@"%.0f",price];
//            cell.goodsInfoLabel.text = model.title;
//            NSLog(@"model:%@",model.goodsId);
//            //已下架
//            if ([model.status isEqualToString:@"0"]) {
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_down"];
//                //新品
//            }else if ([model.status isEqualToString:@"1"]){
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_new"];
//                //热卖中
//            }else if ([model.status isEqualToString:@"2"]){
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_hot"];
//                //缺货中
//            }else if ([model.status isEqualToString:@"3"]){
//                cell.imgNewGoods.image = [UIImage imageNamed:@"goods_Stockout"];
//            }            return cell;
//            
//        }
            break;
        default:
            break;
    }
    return nil;

}
@end
